package com.yc.upload.web;

import com.yc.upload.vo.UploadProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

//将上传到执行目录，目录web访问路径必须是可配置的
@RestController
public class UploadAction {

    @Autowired
    UploadProperties properties;

    @PostMapping("one/{path}")
    public String upload(@RequestParam MultipartFile file,
                         @PathVariable String path) throws IOException {
        String diskPath =properties.getMapping().get(path);
        String filename=file.getOriginalFilename();
        String suffix = filename.substring(filename.lastIndexOf("."));
        String newFileame = UUID.randomUUID() + suffix;
        File fileObj = new File(diskPath,newFileame);
        file.transferTo(fileObj);
        return path+"/"+newFileame;
    }

    @PostMapping("list/{path}")
    public List<String> upload(@RequestParam MultipartFile[] file,
                               @PathVariable String path){


        return null;
    }
}
